<?php

class Bentuk_hukuman extends Controller {

    function Bentuk_hukuman() {
        parent::Controller();    
        $this->load->model('W_bentuk_hukuman_mod', 'u');
        $this->load->model('Datum_model');                        
    }
    
 
    
    function index() {
        if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
            
            $this->list_bentuk_hukuman();
        } 
    }
    

   
    function list_bentuk_hukuman() {
        if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
        }  
        
        $data['additional_js'] = '
        <script type="text/javascript">        
        function deleteRow(id) {
            var txt = \'Anda yakin mau menghapus data ini?<input type="hidden" id="row-id" name="row-id" value="\'+ id +\'" />\';
                url = "'.base_url().'index.php/bentuk_hukuman/hapus/"+id;
             $.ajax({
               type: "POST",
               url: "'.base_url().'index.php/bentuk_hukuman/data_bisa_dihapus/"+id,
               success: function(msg) {
                 if (msg==\'bisa\') {
                    $.prompt(txt,{
                        buttons:{Hapus:true, Batal:false},
                        callback: function(v){
                            if(v) {
                                $("tr#row-"+id).fadeOut("slow", function() {
                                    $("tr#row-"+id).remove();
                                });
                                top.location.href=url;
                            }
                        }
                    });
                 } else {
                    alert(\'Maaf, data ini tidak dapat dihapus dikarenakan sedang digunakan oleh data lain.\');
                 }
               }
            });
        }
         </script> 
        ';
     
          
        $data['list_data'] = $this->u->list_();
        $data['judul'] = 'DAFTAR BENTUK HUKUMAN';
        $data['tampilkan_menu'] = true; 
        $data['aksi'] = 'bentuk_hukuman/tambah';    
        $this->load->view('v_user',$data); 
    }
    
    function tambah() {
        if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
        }  
        
        $data['additional_js'] = '
        <script type="text/javascript">
            $(function() {
            $("#nama").focus();
            $("#btn_kembali").click( function() {
                    window.location = \''.base_url().'index.php/bentuk_hukuman\';
                });
            // validate contact form on keyup and submit
            $("#bentuk_hukuman").validate({
                //set the rules for the field names
                rules: {
                    
                    nama: { required: true },
                    jenis_hukuman: { required: true },

                },
                //set messages to appear inline
                messages: {
                    nama: "Bentuk Hukuman wajib diisi",
                    jenis_hukuman: "Jenis Hukuman wajib diisi",

                }
            });
            
         
        });
        
              
        </script>
';

        $data['additional_css'] = '
            <style type="text/css">
            .error {
                color: red;
                font: 8pt verdana;
                padding-left: 10px
            }
            </style>
        ';
        
        $data['list_data'] = $this->u->entry_('simpan');

        $data['judul'] = 'TAMBAH BENTUK HUKUMAN';
       
        $this->load->view('v_user',$data); 
        
    }
    
    

    function simpan() {
        if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
        }  
        

         $arr['nama'] = $this->input->post('nama');
         $arr['id_jenis_hukuman'] = $this->input->post('jenis_hukuman');
          
       // $kabkota = $this->input->post('kabkota');
        //$id_kabkota =  Datum_model::getData('idspg_kabkota','kabkota','nama ="'.$kabkota.'"');
       
        $this->u->simpan_($arr);                 
        
        redirect('bentuk_hukuman');     
         
    }
    
    function edit($id) {
        if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
        }  
        
        $data['additional_js'] = '
        <script type="text/javascript">
            $(function() {
            $("#nama").focus();
            $("#btn_kembali").click( function() {
                    window.location = \''.base_url().'index.php/bentuk_hukuman\';
                });
            // validate contact form on keyup and submit
            $("#bentuk_hukuman").validate({
                //set the rules for the field names
                rules: {
                    
                    nama: { required: true },
                    jenis_hukuman: { required: true },

                },
                //set messages to appear inline
                messages: {
                    nama: "Bentuk Hukuman wajib diisi",
                    jenis_hukuman: "Jenis Hukuman wajib diisi",

                }
            });
            

        });
        </script>
';

        $this->db->where(array('idspg_bentuk_hukuman' => $id));
        $q = $this->db->get('bentuk_hukuman');
        if ($q->num_rows() > 0) {
            $u = $q->row();
            $arr['nama']= $u->nama;
            $arr['jenis_hukuman'] = $u->id_jenis_hukuman;
        }
            
            
            
        $data['list_data'] = $this->u->entry_('update',$arr,$id);
        $data['judul'] = 'EDIT BENTUK HUKUMAN';
            
        $this->load->view('v_user',$data); 
       
    }
    
    function update() {
        if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
        }  
     
        $id = $this->input->post('id'); 
        $nama = $this->input->post('nama');
        $id_jenis_hukuman = $this->input->post('id_jenis_hukuman');

        $this->u->simpan_($nama,$id_jenis_hukuman,$id,TRUE);                 
        
        redirect('bentuk_hukuman');      
    }
    
    
    
    function hapus($id) {
        if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
        } 
        
        $this->db->where('idspg_bentuk_hukuman', $id);
        $this->db->delete('bentuk_hukuman');
        redirect('bentuk_hukuman');     
        
    }
    
    function data_bisa_dihapus($id) {
        $this->db->where('id_bentuk_hukuman', $id);
        $q = $this->db->get('hukuman');
        if ($q->num_rows() > 0) {
            echo 'tidak';
        } else {
            echo 'bisa';
        }
    }

    
}

